﻿/* **************************************************************************
 *
 * Copyright 2007-2010 Jeff Hardy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * *************************************************************************/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Web.Management.Client.Win32;

namespace NWSGI.ManagementClient
{
    internal partial class WsgiEnvironEditor : TaskForm
    {
        public WsgiEnvironInfo Entry { get; private set; }
        public bool HasChanges { get; private set; }

        private bool canAccept;
        protected override bool CanAccept
        {
            get
            {
                if(base.BackgroundJobRunning)
                    return false;

                return canAccept;
            }
        }

        public WsgiEnvironEditor(IServiceProvider provider, NwsgiModuleServiceProxy proxy) : this(provider, proxy, null) { }

        public WsgiEnvironEditor(IServiceProvider provider, NwsgiModuleServiceProxy proxy, WsgiEnvironInfo entry)
            : base(provider)
        {
            this.proxy = proxy;

            InitializeComponent();

            this.Entry = entry;
            this.inModificationMode = entry != null;
            if(inModificationMode)
            {
                nameBox.Text = this.Entry.Name;
                nameBox.Enabled = false;

                valueBox.Text = this.Entry.Value;
            }

            this.UpdateUIState();
            HasChanges = false;
        }

        private void UpdateUIState()
        {
            HasChanges = true;
            canAccept = !string.IsNullOrEmpty(nameBox.Text.Trim());
            base.UpdateTaskForm();
        }

        protected override void OnAccept()
        {
            WsgiEnvironInfo item = new WsgiEnvironInfo
            {
                Name = nameBox.Text.Trim(),
                Value = valueBox.Text.Trim()
            };

            base.StartAsyncTask(new DoWorkEventHandler(OnWorkerDoWork), new RunWorkerCompletedEventHandler(OnWorkerCompleted), null, item);
        }

        private void OnWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            WsgiEnvironInfo item = (WsgiEnvironInfo)e.Argument;

            if(!inModificationMode)
                this.proxy.AddWsgiEnvironVariable(item);
            else if(HasChanges)
                this.proxy.EditWsgiEnvironVariable(item);

            this.Entry = item;
        }

        private void OnWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error != null)
                this.DisplayErrorMessage(e.Error, Resources.ResourceManager);
            else
            {
                base.Close();
                base.DialogResult = DialogResult.OK;
            }
        }

        private NwsgiModuleServiceProxy proxy;
        private bool inModificationMode;

        private void nameBox_TextChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }

        private void valueBox_TextChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }
    }
}
